﻿/******************************************************************************
 * Product: Adempiere ERP & CRM Smart Business Solution                       *
 * Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved.                *
 * This program is free software; you can redistribute it and/or modify it    *
 * under the terms version 2 of the GNU General Public License as published   *
 * by the Free Software Foundation. This program is distributed in the hope   *
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.           *
 * See the GNU General Public License for more details.                       *
 * You should have received a copy of the GNU General Public License along    *
 * with this program; if not, write to the Free Software Foundation, Inc.,    *
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.                     *
 * For the text or an alternative of this public license, you may reach us    *
 * ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA        *
 * or via info@compiere.org or http://www.compiere.org/license.html           *
 *****************************************************************************/
using System;
using System.Text;
using System.Data;
namespace org.compiere.model{


/**
 * 	Persistent Object LOB.
 * 	Allows to store LOB remotely
 * 	Currently Oracle specific!
 *	
 *  @author Jorg Janke
 *  @version $Id: PO_LOB.java,v 1.2 2006/07/30 00:58:04 jjanke Exp $
 */
public class PO_LOB //implements Serializable
{
	/**
	 * 
	 */
	private static  long serialVersionUID = -325477490976139224L;

	/**
	 * 	Constructor
	 *	@param tableName table name
	 *	@param columnName column name
	 *	@param whereClause where 
	 *	@param displayType display type
	 *	@param value value
	 */
	public PO_LOB (String tableName, String columnName, String whereClause, 
		int displayType, Object value)
	{
		m_tableName = tableName;
		m_columnName = columnName;
		m_whereClause = whereClause;
		m_displayType = displayType;
		m_value = value;
	}	//	PO_LOB

	/**	Logger					*/
	//protected CLogger	log = CLogger.getCLogger (getClass());
	/**	Table Name				*/
	private String 		m_tableName;
	/** Column Name				*/
	private String 		m_columnName;
	/** Where Clause			*/
	private String		m_whereClause;
	
	/** Display Type			*/
	private int			m_displayType;
	/** Data					*/
	private Object 		m_value;

	/**
	 * 	Save LOB
	 * 	@param whereClause clause
	 * 	@param trxName trx name
	 *	@return true if saved
	 */
	public bool save (String whereClause, String trxName)
	{
		m_whereClause = whereClause;
		return save(trxName);
	}	//	save

	/**
	 * 	Save LOB.
	 * 	see also org.compiere.session.ServerBean#updateLOB
	 * 	@param trxName trx name
	 *	@return true if saved
	 */
	public bool save (String trxName)
	{
		if (m_value == null
			|| (!(m_value is String || m_value is byte[])) 
			|| (m_value is String && m_value.ToString().Length == 0)
			|| (m_value is byte[] && ((byte[])m_value).Length == 0)
			)
		{
			StringBuilder sqli = new StringBuilder ("UPDATE ")
				.Append(m_tableName)
				.Append(" SET ").Append(m_columnName)
				.Append("=null WHERE ").Append(m_whereClause);
            int no = 0;//DB.executeUpdate(sqli.ToString(), trxName);
			//log.fine("save [" + trxName + "] #" + no + " - no data - set to null - " + m_value);
            //if (no == 0)
            //    log.warning("[" + trxName + "] - not updated - " + sql);
			return true;
		}
		
		StringBuilder sql = new StringBuilder ("UPDATE ")
			.Append(m_tableName)
			.Append(" SET ").Append(m_columnName)
			.Append("=? WHERE ").Append(m_whereClause);
		//
		
		//log.fine("[" + trxName + "] - Local - " + m_value);
		//	Connection
		//Trx trx = null;
		//if (trxName != null)
		//	trx = Trx.get(trxName, false);
		IDbConnection con = null;
		//	Create Connection
        //if (trx != null)
        //    con = trx.getConnection();
        //if (con == null)
        //    con = null;//DB.createConnection(false, Connection.TRANSACTION_READ_COMMITTED);
        //if (con == null)
        //{
        //    log.log(Level.SEVERE, "Could not get Connection");
        //    return false;
        //}

        //PreparedStatement pstmt = null;
        //bool success = true;
        //try
        //{
        //    pstmt = con.prepareStatement(sql.ToString());
        //    if (m_displayType == DisplayType.TextLong)
        //        pstmt.setString(1, (String)m_value);
        //    else
        //        pstmt.setBytes(1, (byte[])m_value);
        //    int no = pstmt.executeUpdate();
        //    if (no != 1)
        //    {
        //        log.warning("[" + trxName + "] - Not updated #" + no + " - " + sql);
        //        success = false;
        //    }
        //}
        //catch (Throwable e)
        //{
        //    log.log(Level.SEVERE, "[" + trxName + "] - " + sql, e);
        //    success = false;
        //}
        //finally
        //{
        //    DB.close(pstmt);
        //    pstmt = null;
        //}
		
		//	Success - commit local trx
        //if (success)
        //{
        //    if (trx != null)
        //    {
        //        trx = null;
        //        con = null;
        //    }
        //    else
        //    {
        //        try
        //        {
        //            con.commit();					
        //        }
        //        catch (Exception e)
        //        {
        //            log.log(Level.SEVERE, "[" + trxName + "] - commit " , e);
        //            success = false;
        //        }
        //        finally
        //        {
        //            try {
        //                con.close();
        //            } catch (SQLException e) {
        //            }
        //            con = null;
        //        }
        //    }
        //}
        ////	Error - roll back
        //if (!success)
        //{
        //    log.severe ("[" + trxName + "] - rollback");
        //    if (trx != null)
        //    {
        //        trx.rollback();
        //        trx = null;
        //        con = null;
        //    }
        //    else
        //    {
        //        try
        //        {
        //            con.rollback();
        //        }
        //        catch (Exception ee)
        //        {
        //            log.log(Level.SEVERE, "[" + trxName + "] - rollback" , ee);
        //        }
        //        finally
        //        {
        //            try {
        //                con.close();
        //            } catch (SQLException e) {
        //            }
        //            con = null;
        //        }
        //    }
        //}
		
        //return success;
        return true;
	}	//	save

	
	/**
	 * 	String Representation
	 *	@return info
	 */
	public String ToString()
	{
		StringBuilder sb = new StringBuilder("PO_LOB[");
		sb.Append(m_tableName).Append(".").Append(m_columnName)
			.Append(",DisplayType=").Append(m_displayType)
			.Append("]");
		return sb.ToString();
	}	//	toString

}	//	PO_LOB

}